import java.util.Stack;

public class Leet907 {
    public static void main(String[] args) {
        Leet907 l = new Leet907();
        System.out.println(l.sumSubarrayMins(new int[]{3,2,3,1}));
    }
    private final int mod = (int) 1e9 + 7;

    public int sumSubarrayMins(int[] arr) {
        int len = arr.length;
        if (len == 1)
            return arr[0];
        Stack<Integer> s = new Stack<>();
        long ans = 0;
        for (int i = 0; i < len; i++) {
            while (!s.isEmpty() && arr[s.peek()] >= arr[i]) {
                Integer index = s.pop();
                int rangeLeft = s.isEmpty() ? -1 : s.peek();
                ans += (long) (index - rangeLeft) * (i - index) * arr[index];
                ans %= mod;
            }
            s.add(i);
        }
        int t = s.size();
        for (int i = 0; i < t; i++) {
            System.out.println(arr[s.pop()]);
        }
        while (!s.isEmpty()) {
            Integer index = s.pop();
            int rangeLeft = s.isEmpty() ? -1 : s.peek();
            ans += (long) (index - rangeLeft) * (len - index) * arr[index];
            ans %= mod;
        }
        return (int) (ans % mod);
    }
}
